<?php 
namespace Home\Model;
use Think\Model;
class AddressModel extends Model {
	public function validate(){
		 $id = I('post.id',0,'intval');               //地址id
		 $uid = I('post.uid',0,'intval');             //会员id
		 $true_name = I('post.true_name','');            //会员姓名
		 $area_id = I('post.area_id',0,'intval');     //地区id
		 $city_id = I('post.city_id',0,'intval');     //城市id
		 $address = I('post.address','');                //街道
		 $mob_phone = I('post.mob_phone','');            //手机号
		 $is_default = I('post.is_default') ? 1 : 0;  //是否默认地址
			
		 //print_r(I('post.'));
		 //数据验证
		 if(!$true_name)  returnApiError(1,'请填写会员姓名');
		 if(!$area_id)  returnApiError(1,'地区id参数错误');
		 if(!$city_id)  returnApiError(1,'城市id参数错误');
		 if(!$address)  returnApiError(1,'请填写街道详细地址');
		 if(!$mob_phone)  returnApiError(1,'请填写手机号');
		 if(strlen($true_name) > 50)  returnApiError(1,'姓名长度超过50个字符');
		 if(strlen($address) > 255)  returnApiError(1,'街道地址长度超过255个字符');
		 if(strlen($mob_phone) > 15)  returnApiError(1,'街道地址长度超过15个字符');
		 if(!$this->where("member_id = $uid")->find()) returnApiError(4006,'用户不存在');
		 
		 //实例化地区
		 $Area = M('Area');
		 
		 //市
		 $city_where['area_id'] = $city_id;
		 $city = $Area->where($city_where)->find();
		 $city_name  = $city['area_name'];
		 $pro_id = $city['area_parent_id'];
		 
		 //区
		 $area_where['area_id'] = $area_id;
		 $city = $Area->where($area_where)->find();
		 $area_name  = $city['area_name'];
		 
		 //省
		 $pro_where['area_id'] = $pro_id;
		 $pro = $Area->where($pro_where)->find();
		 $pro_name  = $pro['area_name'];
		 for($i=0;$i<=3;$i++){
			  $txt .= " ";	
		 }
		 $area_info = $pro_name.$txt.$city_name.$txt.$area_name;
		 
		 $where['address_id'] = $id;
		 $where['member_id'] = $uid;
		 $data['true_name'] = $true_name;
		 $data['area_id'] = $area_id;
		 $data['city_id'] = $city_id;
		 $data['address'] = $address;
		 $data['mob_phone'] = $mob_phone;
		 $data['area_info'] = $area_info;
		 $data['member_id'] = $uid;
		 if($is_default)  $data['is_default'] = $is_default;
		
		 $arr = array(
			'data' => $data,
			'where' => $where,
		 );
		 return $arr;
	}
}